const {
  GiftShopModel
} = require('../../../utils/giftshop-model.js')
const giftShopModel = new GiftShopModel()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    historySearch: [],
    historyShowMore: false,
    hotSearch: [],
    searchResult: [],
    pageIndex: 1,
    pageSize: 50,
    totalCount: 0
  },
  onLoad() {
    const historySearch = wx.getStorageSync('historySearch') || []
    this.setData({
      historySearch
    })
    this.getHotSearch()
  },
  showMore() {
    this.setData({
      historyShowMore: true
    })
  },
  closeMore() {
    this.setData({
      historyShowMore: false
    })
  },
  getHotSearch() {
    const merchantId = wx.getStorageSync('merchantId')
    giftShopModel.getHotKeywords({
      merchantId
    }).then(data => {
      this.setData({
        hotSearch: data.key_words || []
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },
  getSearchResult() {
    const merchantId = wx.getStorageSync('merchantId')
    const shopInfo = wx.getStorageSync('shopInfo')
    giftShopModel.getSearchResult({
      merchantId,
      shopId: shopInfo.shopId,
      keyword: this.data.keyword,
      offset: (this.data.pageIndex - 1) * this.data.pageSize,
      limit: this.data.pageSize
    }).then(data => {
      this.setData({
        searchResult: data.data || [],
        totalCount: data.totalCount
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },
  search(value) {
    let historySearch = [...this.data.historySearch]
    const index = historySearch.indexOf(value)
    if (index !== -1) {
      historySearch.splice(index, 1)
    }
    if (historySearch.length === 30) {
      historySearch.pop()
    }
    historySearch.unshift(value)
    this.setData({
      historySearch,
      pageIndex: 1
    })
    wx.setStorageSync('historySearch', historySearch)
    // this.getSearchResult()
    this.goSearch()
  },
  goSearch() {
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    prevPage.setData({
      navigateCode: '',
      keyword: this.data.keyword || '',
      pageIndex: 1,
      tabIndex: 0,
      sortBy: 0,
      sortOrder: 'desc'
    }, () => {
      prevPage.getSearchResult().then(data => {
        prevPage.setData({
          searchResult: data.data || [],
          totalCount: data.totalCount
        })
      })
    })
    wx.navigateBack({
      success () {
        wx.setNavigationBarTitle({
          title: ''
        })
      }
    })
  },
  scrollEnd() {
    if (this.data.searchResult.length < this.data.totalCount) {
      this.setData({
        pageIndex: this.data.pageIndex + 1
      })
      const merchantId = wx.getStorageSync('merchantId')
      const shopInfo = wx.getStorageSync('shopInfo')
      giftShopModel.getSearchResult({
        merchantId,
        shopId: shopInfo.shopId,
        keyword: this.data.keyword,
        offset: (this.data.pageIndex - 1) * this.data.pageSize,
        limit: this.data.pageSize
      }).then(data => {
        this.setData({
          searchResult: this.data.searchResult.concat(data.data || []),
          totalCount: data.totalCount
        })
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none'
        })
      })
    }
  },
  clearHistory() {
    this.setData({
      historySearch: []
    })
    wx.reportAnalytics('globalsearch_clearhistory')
  },
  inputValue(e) {
    wx.reportAnalytics('globalsearch_menu')
    const { item } = e.currentTarget.dataset
    this.setData({
      keyword: item
    })
    this.search(item)
  },
  inputKeyword(e) {
    wx.reportAnalytics('globalsearch_search')
    let { value } = e.detail
    value = value.replace(/^(\s|\u00A0)+/,'').replace(/(\s|\u00A0)+$/,'')
    this.setData({
      keyword: value
    })
    if (value) {
      this.search(value)
    }
  },
  cancel() {
    wx.reportAnalytics('globalsearch_cancel')
    this.setData({
      keyword: ''
    })
  }
})